// $(".weui-loadmore").hide();
//订单加载
//加载中标记loading
//加载更多loadMoreShow
//绑定滚动加载事件
//加载更多 loadmore()
var loading = false;
var loadMoreShow = true;
var orderIndex = 1; //订单页index
$(window).bind("scroll", function() {
    if($(document).scrollTop() + $(window).height() > $(document).height() - 10) {
        loadmore();
    }else{
        return false
    }
});
function loadmore() {
    if (loading == false) {
        loading = true;
        if (loadMoreShow == true) {
            $(".weui-loadmore").show();
        }
        setTimeout(function() {
            orderIndex++;
            // console.log(orderIndex)
            getOrderDetail(Types);
        }, 1500); //模拟延迟
    }
};
$(".my_order #go_back").click(function() {
        window.location.href = "../index.html";
    })
// 订单管理
// orderType 订单类型  orderStatus 订单状态
// 4种订单类型 type数组：sdType
var userInfo = localStorage.getItem("userInfo");
var userInfoObj = JSON.parse(userInfo);
if (userInfoObj) {
    var UserId = userInfoObj.data.UserId;
}
var Types = sessionStorage.getItem("OrderType") || 0;
// 初始化选择类型 
function changeType(Types) {
    //重置数据
    //相关样式改变
    //发送请求
    loadMoreShow = true;
    orderIndex = 1;
    var selectType = Types;
    if (selectType == "5") {
        selectType = 3;
    }
    sessionStorage.setItem("OrderType", Types);
    $(".order_type span").css("border-color", "transparent").eq(selectType).css("border-color", "#121212");
    $(".weui-loadmore").show();
    $("#orderList .order_box").remove();
    $(document).scrollTop(10);
    getOrderDetail(Types);
}
changeType(Types);
$(".sdBuy").click(function() {
    changeType(0)
})
$(".sdDeliver").click(function() {
    changeType(1);
})
$(".sdTake").click(function() {
    changeType(2);
})
$(".sdQueue").click(function() {
    changeType(5);
})
//检查是否有订单信息;
function checkOrder() {
    if (!$(".order_box").length) {
        return $(".no_order").show();
    } else {
        return $(".no_order").hide();
    }
}
//发送请求
function getOrderDetail(Types) {
    //买送取发送请求
    //变量根据类型类型会发生改变
    if (Types == 0 || Types == 1 || Types == 2) {
        ajaxUrl = currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_OrderManage_List";
        var getStr = function getOrderStr(data, i) {
            var orderStatusStr = "";
            if (data.data[i].Statsus == "0") {
                orderStatusStr = "等待抢单";
            } else if (data.data[i].Statsus == "1" || data.data[i].Statsus == "2" || data.data[i].Statsus == "3" || data.data[i].Statsus == "10") {
                orderStatusStr = "已抢单";
            } else if (data.data[i].Statsus == "4" || data.data[i].Statsus == "5" || data.data[i].Statsus == "6") {
                orderStatusStr = "已取货";
            } else if (data.data[i].Statsus == "7") {
                orderStatusStr = "已送达";
            }
            var orderStr = "";
            orderStr += "<div class='order_box'>";
            orderStr += "<div class='flex_row tit'>";
            orderStr += "<i class='iconfont icon-qubiezhen'></i>";
            orderStr += "<p>订单状态:</p>";
            orderStr += "<span id='order_sta'>" + orderStatusStr + "</span>";
            orderStr += "</div><div class='add_mes row'>";
            orderStr += "<p><i class='iconfont icon-dian-copy'></i>";
            orderStr += "<span id='startAdd'>" + data.data[i].QIAddress + "</span></p>";
            orderStr += "<p><i class='iconfont icon-dian-copy'></i>";
            orderStr += "<span id='endAdd'>" + data.data[i].ZhongAddress + "</span></p>";
            orderStr += "</div><div class='order_mes row'>";
            orderStr += "<p><span id='tel_name'>收货电话</span><span id='tel'>" + data.data[i].ZhongTel + "</span></p>";
            orderStr += "<p><span id='distance'>路程大约" + data.data[i].SongJuLi + "公里,</span><span id='orderMoney'>费用" + data.data[i].Money + "元</span></p>";
            // orderStr += "<span class='assess' style='color:#333;display:inline-block;width:1.2rem;text-align:center;z-index:109;padding:.02rem;border:1px solid #333;border-radius:10%;'>评价</span>";
            orderStr += "</div></div>";
            return orderStr
        }
    } else if (Types == 5) {
        ajaxUrl = currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_OrderListBP";
        var getStr = function getOrderStr(data, i) {
            var orderStr = "";
            var orderStatusStr = "";
            if (data.data[i].Statsus == "0") {
                orderStatusStr = "等待抢单";
            } else if (data.data[i].Statsus == "1" || data.data[i].Statsus == "2" || data.data[i].Statsus == "3" || data.data[i].Statsus == "10") {
                orderStatusStr = "已抢单";
            } else if (data.data[i].Statsus == "4" || data.data[i].Statsus == "5" || data.data[i].Statsus == "6") {
                orderStatusStr = "已抢单";
            } else if (data.data[i].Statsus == "7") {
                orderStatusStr = "已完成";
            }
            orderStr = "<li class='order_box flex_column'>" +
            "<div class='flex_row tit'>" +
            "<i class='iconfont icon-qubiezhen'></i>" +
            "<p>订单状态:</p>" +
            "<span id='order_sta'>" + orderStatusStr + "</span>" +
            "</div><div class='add_mes row'>" +
            "<p><i class='iconfont icon-dian-copy'></i>" +
            "<span id='startAdd'>排队信息:" + data.data[i].BangXinxi + "</span></p>" +
            "<p><i class='iconfont icon-dian-copy'></i>" +
            "<span id='endAdd'>排队时长:" + data.data[i].BangShiChang + "分钟</span></p>" +
            "</div><div class='order_mes row'>" +
            "<p><span id='tel_name'>联系电话</span><span id='tel'>" + data.data[i].DownTel + "</span></p>" +
            "<p><span id='distance'>路程大约" + data.data[i].QuJuLi + "公里,</span><span id='orderMoney'>费用" + data.data[i].Money + "元</span></p>"+
            "</div></li>";
            return orderStr;
        }
    }
    $.ajax({
            type: "get",
            dataType: "json",
            url: ajaxUrl,
            data: {
                "index": orderIndex,
                "Types": Types,
                "Status": "9",
                "UserId": UserId
            },
            success: function(data) {
                console.log(data);
                if (data.msgcode == "1") {
                    var dataLen = data.data.length;
                    //新加载出来的订单数
                    if (orderIndex <= 1) {
                        var orderIndexNum = (orderIndex - 1) * dataLen;
                    } else {
                        var orderIndexNum = (orderIndex - 1) * 6;
                    }
                    if (dataLen != 0) {
                        //插入订单
                        for (var k = 0; k < dataLen; k++) {
                            // 订单状态
                            $("#orderList").append(getStr(data, k));
                            //oi ： 订单的index;
                            var oi = k + orderIndexNum;
                            $(".order_box").eq(oi).data("OrderId", data.data[k].OrderId);
                            $(".order_box").eq(oi).data("OrderType", data.data[k].Type);
                            $(".order_box").eq(oi).data("OrderStatsus", data.data[k].Statsus);
                            setOrderClick();
                        }
                        //将加载更多插入到尾部
                        var orderNum = $(".order_box").length;
                        $(".order_box").eq(orderNum - 1).after($(".weui-loadmore"));
                    } else {
                        if (orderIndex > 1) {
                            loadMoreShow = false;
                        }
                    }
                } else {
                    $.alert(data.msg);
                }
            },
            error: function() {
                console.log("加载订单错误");
            },
            complete: function() {
                checkOrder();
                $(".weui-loadmore").hide();
                loading = false;
            }
        })
}
// 设置订单点击功能；
function setOrderClick() {
    $(".order_box").click(function() {
        var OrderId = $(this).data("OrderId");
        var OrderType = $(this).data("OrderType");
        var OrderStatsus = $(this).data("OrderStatsus")
        sessionStorage.setItem("OrderId", OrderId);
        sessionStorage.setItem("OrderType", OrderType);
        if (OrderStatsus == 0) {
            window.location.href = "../html/order_wait.html";
        } else {
            window.location.href = "../html/order_status.html";
        }
    })
}
$("#orderList").on("click",".assess",function(){
    console.log("111");
    return false
})